/**
 * Copyright 2010 Adam Ruggles.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package saig.engine;

import org.kohsuke.args4j.Option;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import saig.engine.controllers.Controller;
import saig.engine.controllers.HumanController;

/**
 * Settings for the game.
 * @author Adam
 * @version $Id$
 * 
 * Created on Aug 20, 2010 at 9:02:23 PM 
 */
public final class Settings {
	/**
	 * The default time on the clock per level.
	 */
	public static final int CLOCK = 300;
	/**
	 * The number game clock ticks per second.
	 */
	public static final float CLOCK_TICKS_PER_SECOND = 1.5f;
	/**
	 * The default frames per second.
	 */
	public static final int FPS = 30;
	/**
	 * The default screen height in pixels.
	 */
	public static final int SCREEN_HEIGHT = 224;
	/**
	 * The default screen width in pixels.
	 */
	public static final int SCREEN_WIDTH = 256;
	/**
	 * The time available to complete a level.
	 */
	@Option(name = "-tl", usage = "Time Limit. The amount of time on the clock.")
	private int clock = CLOCK;
	/**
	 * The fqn of the controller class to use.
	 */
	@Option(name = "-cc", usage = "")
	private String controllerClassName = "saig.engine.controllers.HumanController";
	/**
	 * The number of frames per second.
	 */
	@Option(name = "-fps",
			usage = "Frames Per Second. The number of frames per second (speeds up the visual simulation).")
	private int fps = FPS;
	/**
	 * Logger.
	 */
	private final Logger logger = LoggerFactory.getLogger(Settings.class);
	/**
	 * The screen width in pixels.
	 */
	private int screenHeight = SCREEN_HEIGHT;
	/**
	 * The screen width in pixels.
	 */
	private int screenWidth = SCREEN_WIDTH;
	/**
	 * Default constructor, sets all values using the defaults.
	 */
	public Settings() { }
	/**
	 * Returns clock.
	 * @return the clock.
	 */
	public int getClock() {
		return clock;
	}
	/**
	 * Returns the controller.
	 * @return The controller.
	 */
	@SuppressWarnings("unchecked")
	public Controller getController() {
		Class<Controller> theClass = null;
		try {
		    theClass = (Class<Controller>) Class.forName( controllerClassName, true,
		    		Thread.currentThread().getContextClassLoader());
		}
		catch (ClassNotFoundException e) {
			logger.warn("Unable to load class, trying second method.", e);
		    try {
				theClass = (Class<Controller>) Class.forName( controllerClassName );
			} catch (final ClassNotFoundException e1) {
				logger.error("Unable to load class using default human controller.", e1);
				return new HumanController();
			}
		}
		try {
			return (Controller) theClass.newInstance();
		} catch (final InstantiationException e) {
			logger.error("Unable to instantiate the controller.", e);
			return new HumanController();
		} catch (final IllegalAccessException e) {
			logger.error("Unable to instantiate the controller.", e);
			return new HumanController();
		}
	}
	/**
	 * Returns controllerClassName.
	 * @return the controllerClassName.
	 */
	public String getControllerClassName() {
		return controllerClassName;
	}
	/**
	 * Returns fps.
	 * @return the fps.
	 */
	public int getFps() {
		return fps;
	}
	/**
	 * Returns screenHeight.
	 * @return the screenHeight.
	 */
	public int getScreenHeight() {
		return screenHeight;
	}
	/**
	 * Returns screenWidth.
	 * @return the screenWidth.
	 */
	public int getScreenWidth() {
		return screenWidth;
	}
	/**
	 * Sets clock.
	 * @param clock the clock to set.
	 */
	public void setClock(int clock) {
		this.clock = clock;
	}
	/**
	 * Sets controllerClassName.
	 * @param controllerClassName the controllerClassName to set.
	 */
	public void setControllerClassName(String controllerClassName) {
		this.controllerClassName = controllerClassName;
	}
	/**
	 * Sets fps.
	 * @param fps the fps to set.
	 */
	public void setFps(int fps) {
		this.fps = fps;
	}
	/**
	 * Sets screenHeight.
	 * @param screenHeight the screenHeight to set.
	 */
	public void setScreenHeight(int screenHeight) {
		this.screenHeight = screenHeight;
	}
	/**
	 * Sets screenWidth.
	 * @param screenWidth the screenWidth to set.
	 */
	public void setScreenWidth(int screenWidth) {
		this.screenWidth = screenWidth;
	}
}
